package com.sczmgk.track.utils;

import android.content.Context;

import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.sczmgk.track.MyApp;
import com.sczmgk.track.db.impl.UserDaoImpl;
import com.sczmgk.track.entity.User;
import com.sczmgk.track.httpUtils.MyHttp;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/5/25.
 */

public class UserImageUtils {

    private static final String TAG = UserImageUtils.class.getSimpleName();

    public static void downLoadPic(final Context context, final PictureUtil.OnsavePicListener onsavePicListener) {

        final User user = UserDaoImpl.getLoginUser(context);
        if(user == null){
            MyLog.e(TAG,"用户没登录，不用下载图片");
            return;
        }
        MyLog.e(TAG, "没有取到用户图5片，去下载: url：" + user.headimg);
//        uploadUserName();
        String path = FileUtil.getUserCaptureCacheFilePath(user.userid+"");
        if(Tools.isEmpty(path)){
            return;
        }
        if(Tools.isEmpty(user.headimg)){
            if (onsavePicListener != null) {
                onsavePicListener.onFailure("图片下载失败");
            }
            return;
        }

         MyHttp.downloadFile(user.headimg, path, new RequestCallBack<File>() {
             @Override
             public void onSuccess(ResponseInfo<File> responseInfo) {
                 user.imageLocalPath = FileUtil.getUserCaptureCacheFilePath(MyApp.loginUserUid + "");
                 UserDaoImpl.getIntance(context).updata(user);
                 MyLog.d(TAG, "图片下载成功");
                 if (onsavePicListener != null) {
                     onsavePicListener.onSuccess();
                 }
             }

             @Override
             public void onFailure(HttpException error, String msg) {
                 MyLog.d(TAG,"图片下载失败");
                 if (onsavePicListener != null) {
                     onsavePicListener.onFailure("图片下载失败");
                 }
             }
         });
    }

    public static void uploadUserName() {
        StringBuilder sb = new StringBuilder();
        sb.append("userid: "+MyApp.loginUserName);
        sb.append("\n");

        Map<String,Object> exceptionMaps = new HashMap<String,Object>();
        exceptionMaps.put("exceptiontype", 1);
        exceptionMaps.put("exceptioncontent",sb.toString() );
        //TODO 上传异常
        //"http://www.joygov.com:8010/api/v1/"
        //"Files/AddException"

        String url ="http://bobo023.xyz/appapi/v1/uploadusername";
//			String url = "http://www.joygov.com:8010/api/v1/Files/AddException";
//        MyHttp.reportException(url,exceptionMaps);
    }
}
